package comman.duke 
{
	import consts.PokerGameVars;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author jerry.d
	 */
	public class SoundMgr 
	{
		private var bg:Sound;
		private var bgChannel:SoundChannel;
		private var bgTransform:SoundTransform;
		private var effectTransform:SoundTransform;
		
		public function SoundMgr() 
		{
			this.bgTransform = new SoundTransform(0.1);
			this.effectTransform = new SoundTransform();
			effectTransform.volume = 1;
			this.bg = new Sound();
		}
		
		private var voiceOn:Boolean = true;
		private var musicOn:Boolean = true;
		public function musicSwitch(value:Boolean):void{
			musicOn = value;
			if ( musicOn ){
				this.bgChannel = this.bg.play();
			}else{
				this.bgChannel.stop();
			}
		}
		
		public function voiceSwitch(value:Boolean):void{
			voiceOn = value;
		}
		
		public function playBg(url:String):void{
			bg.load(new URLRequest(PokerGameVars.resRoot+url+'?v='+PokerGameVars.VERSION));
			bgChannel = bg.play(0, 9999, bgTransform);
		}
		
		private var effectMap:Object = {};
		private var voicePlaying:Boolean;
		private var lastEffectChanell:SoundChannel;
		public function playVoice(url:String, deathVoice:Boolean = false):void{
			GameUtils.log('SoundMgr.playVoice:', url, 'deathVoide-'+deathVoice,'voiceOn-'+voiceOn);
			if ( !voiceOn ) {
				if (deathVoice){
					GameMgr.Instance.onRoundEnd();
				}
				return;
			}
			if ( !effectMap.hasOwnProperty(url)){
				effectMap[url] = [];
			}
		
			if ( voicePlaying && lastEffectChanell != null ){
				lastEffectChanell.stop();//需要回收不，会不会泄漏
				voicePlaying = false;
			}
			
			var effect:Sound;
			if ( effectMap[url].length != 0){
				effect = effectMap[url].pop();
			}else{
				effect = new Sound(new URLRequest(PokerGameVars.resRoot+url+'?v='+PokerGameVars.VERSION));
			}
			
			voicePlaying = true;
			lastEffectChanell = effect.play(0, 0, effectTransform);
			
			lastEffectChanell.addEventListener(Event.SOUND_COMPLETE, function(e:Event):void{
				if( effectMap[url].indexOf(effect) == -1)
					effectMap[url].push(effect);
					
				GameUtils.log('voice complete:', url, 'deathVoide-'+deathVoice);
				if ( deathVoice ){
					GameMgr.Instance.onRoundEnd();
				}
				
				voicePlaying = false;
			});
		}
		
		
		private var effectQueue:Array = [];
		public function playEffect(url:String):void{
			if ( !voiceOn ) {
				return;
			}
			
			if ( !effectMap.hasOwnProperty(url)){
				effectMap[url] = [];
			}
			
			var effect:Sound;
			if ( effectMap[url].length != 0){
				effect = effectMap[url].pop();
			}else{
				effect = new Sound(new URLRequest(PokerGameVars.resRoot+url+'?v='+PokerGameVars.VERSION));
			}
			
			var channel:SoundChannel = effect.play(0, 0, effectTransform);
			channel.addEventListener(Event.SOUND_COMPLETE, function(e:Event):void{
				if( effectMap[url].indexOf(effect) == -1)
					effectMap[url].push(effect);
			});
		}
		
		private static var _instance:SoundMgr;
		public static function get Instance():SoundMgr{
			if ( SoundMgr._instance == null){
				SoundMgr._instance = new SoundMgr();
			}
			return SoundMgr._instance;
		}
	}

}